package main

import (
	_ "embed"
	"fmt"
	"html/template"
	"net/url"
	"os"
	"path/filepath"
	"strconv"
	"time"
)

//go:embed line.html
var lineTpl string

func GetChart(w []*ChartPair, l []*ChartPair, w2 []*ChartPair, l2 []*ChartPair) {
	text := bc.OutChart.Text

	formartTime := ToUnix(time.Now())

	out := filepath.Join(text, "chart"+formartTime+".html")
	bc.Log("图表输出:" + out)
	files, e := template.New("line").Parse(lineTpl)

	if e != nil {
		bc.Log("图表输出失败:" + e.Error())
		return
	}
	l = l[1:]
	l2 = l2[1:]
	temp, _ := os.Create(out)

	var xaxis []string

	for _, v := range w {
		xaxis = append(xaxis, v.Time)
	}

	xaxis = getSoftArr(xaxis)

	var loseAxisZheng []float64
	var loseAxisFu []float64

	for _, v := range l {
		t, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.Value), 64)
		loseAxisZheng = append(loseAxisZheng, t)
	}

	for _, v := range l2 {
		t, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.Value), 64)
		loseAxisFu = append(loseAxisFu, 0-t)
	}

	var winYaxisZheng []float64
	var winYaxisFu []float64

	for _, v := range w {
		t, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.Value), 64)
		winYaxisZheng = append(winYaxisZheng, t)
	}

	for _, v := range w2 {
		t, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.Value), 64)
		winYaxisFu = append(winYaxisFu, 0-t)
	}

	files.Execute(temp, struct {
		Xaxis          []string
		WinYaxisZheng  []float64
		WinYaxisFu     []float64
		LoseYaxisZheng []float64
		LoseYaxisFu    []float64
	}{
		Xaxis:          xaxis,
		WinYaxisZheng:  winYaxisZheng,
		WinYaxisFu:     winYaxisFu,
		LoseYaxisZheng: loseAxisZheng,
		LoseYaxisFu:    loseAxisFu,
	})

	u, _ := url.Parse(filepath.Join("file:", out))
	btcApp.OpenURL(u)
}
